﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModelORM;
using System.Data.Entity;

namespace Zarzadzacz
{
    public class KadraZarzadzator : Zarzadzator<tbl_kadra>
    {

        //Implementacja metod interfejsu IData
        public override List<tbl_kadra> GetData()
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                List<tbl_kadra> lista;
                try
                {
                    lista = (from p in context.tbl_kadra
                             select p).ToList();

                }
                catch (Exception ex)
                {
                    lista = new List<tbl_kadra>();
                }

                return lista;
            }

        }

        public override tbl_kadra GetData(int dataId)
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                tbl_kadra entity;
                try
                {
                    entity = (from k in context.tbl_kadra
                                  where k.ID == dataId
                                  select k).FirstOrDefault();

                    
                }
                catch(Exception ex)
                {
                    entity = null;
                }

                return entity;
            }
        }

        //implementacja metod interfejsu IUpdate
        public override bool UpdateData(tbl_kadra dataToUpdate)
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                try
                {
                    var entity = (from k in context.tbl_kadra
                                  where k.ID == dataToUpdate.ID
                                  select k).FirstOrDefault();

                    //imie i nazwisko bez możliwości zmiany
                    entity.Haslo = dataToUpdate.Haslo;
                    entity.IDRoli = dataToUpdate.IDRoli;                 
                    entity.Login = dataToUpdate.Login;

                    context.SaveChanges();
                    return true;
                    
                }
                catch (Exception ex)
                {
                    //obsluga wyjątku ...
                    return false;
                }
            }
        }

        public override bool DeleteData(int dataId)
        {
            using (MagazynEntities context = new MagazynEntities())
            {
                try
                {
                    var entity = (from k in context.tbl_kadra
                                  where k.ID == dataId
                                  select k).FirstOrDefault();
                    context.tbl_kadra.DeleteObject(entity);
                    context.SaveChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        

    }
       




    

   
}
